package com.csun.nusing.familypresenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FamilyOlderListDateJsonBean;
import com.csun.nusing.bean.jsonbean.FamilyOlderListDateJsonBean.FamilyOlderListDataBean;
import com.csun.nusing.bean.jsonbean.FileSearchJsonBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean;
import com.csun.nusing.presenter.BasePresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.adapter.FamilyOldrestAdapter;
import com.csun.nusing.ui.familyactivity.FamilyOldrestDetailsActivity;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在护工主界面的第三个页面--老人页面fragment的presenter层 处理里面的业务逻辑 ClassName:
 * FragmentNursingOldrestPresenter
 * 
 * @Description: 老人管理
 * @author fzq
 * @date 2017-10-20
 */
public class FragmentFamilyOldrestPresenter extends BasePresenter<IBase> {

	private String authorization;
	private String userid;
	protected NursingOldrestListJsonBean nursingoldrestlistbean;
	protected FileSearchJsonBean fillsearchbean;
	protected int olderId;
	protected String olderName;
	// private RecycleViewAdapter mAdapter;
	protected int olderPhotoId;
	protected String olderheadPicture;
	protected FamilyOlderListDateJsonBean familyoldrestlistbean;
	protected String roomCode;

	public FragmentFamilyOldrestPresenter(IBase view) {
		super(view);
	}

	/**
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @param @param item
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-21
	 */
	public void clickButton(Context context, String item) {

	}

	/**
	 * 获取看护老人列表数据
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	public void getOldListDate(final Context context, final GridView gridView) {
		authorization = (String) SPUtils.get(context, "authorization", "");
		userid = (String) SPUtils.get(context, "userId", "");
		Map<String, String> param = new HashMap<String, String>();
		OkHttpTask.newInstance(context).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							familyoldrestlistbean = gson.fromJson(result,
									FamilyOlderListDateJsonBean.class);
							if (familyoldrestlistbean != null) {
								if (familyoldrestlistbean.getData() != null) {
									controlRecycleView(context,
											familyoldrestlistbean.getData(),
											gridView);
								}
							}
						}
					}
				}).start(UrlBean.FAMILYOLDERLISTURL);
	}

	/**
	 * 操作数据,添加到gridview中
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @param @param nursingoldrestlistbean2
	 * @param @param gridView
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-27
	 */
	protected void controlRecycleView(final Context context,
			final List<FamilyOlderListDataBean> list, GridView gridview) {
		if (list.size() == 0) {
			return;
		}
		FamilyOldrestAdapter oldrestAdapter = new FamilyOldrestAdapter(context,
				list);
		gridview.setAdapter(oldrestAdapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(context,
						FamilyOldrestDetailsActivity.class);
				olderId = list.get(position).getId();
				olderName = list.get(position).getName();
				olderheadPicture = list.get(position).getHeadPicture();
				roomCode = "-";
				if (list.get(position).getBuildingName() == null) {
					roomCode = "-";
				} else if (list.get(position).getRoomNo() == null) {
					roomCode = "-";
				} else if (list.get(position).getBedNo() == null) {
					roomCode = "-";
				} else if (list.get(position).getBuildingName().equals("null")) {
					roomCode = "-";
				} else if (list.get(position).getRoomNo().equals("null")) {
					roomCode = "-";
				} else if (list.get(position).getBedNo().equals("null")) {
					roomCode = "-";
				} else {
					roomCode = list.get(position).getBuildingName()
							+ list.get(position).getRoomNo() + "房"
							+ list.get(position).getBedNo() + "床";
				}
				intent.putExtra("olderId", "" + olderId);
				intent.putExtra("olderPhotopath", "" + olderheadPicture);
				intent.putExtra("olderName", "" + olderName);
				intent.putExtra("bedNo", "" + ""+list.get(position).getBedNo());
				intent.putExtra("roomId", "" + list.get(position).getRoomId());
				intent.putExtra("olderBir", ""
						+ list.get(position).getBirthday());
				intent.putExtra("olderage", "" + list.get(position).getAge());
				intent.putExtra("olderSex", "" + list.get(position).getSex());
				intent.putExtra("olderEcgname", ""
						+ list.get(position).getEgContact());
				intent.putExtra("olderEcgPhone", ""
						+ list.get(position).getEgPhone());
				intent.putExtra("roombed",roomCode);
				context.startActivity(intent);
			}
		});
	}

}
